package com.share.rule.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.common.core.exception.ServiceException;
import com.share.common.core.utils.bean.BeanUtils;
import com.share.common.security.utils.SecurityUtils;
import com.share.rule.domain.*;
import com.share.rule.mapper.FeeRuleMapper;
import com.share.rule.service.IFeeRuleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/10/14 上午9:25
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class FeeRuleServiceImpl extends ServiceImpl<FeeRuleMapper, FeeRule> implements IFeeRuleService {

    private final FeeRuleMapper feeRuleMapper;
    private final KieContainer kieContainer;

    /**
     * 查询费用规则列表
     *
     * @param feeRule
     * @return
     */
    @Override
    public List<FeeRule> selectFeeRuleList(FeeRule feeRule) {
        return feeRuleMapper.selectFeeRuleList(feeRule);
    }

    /**
     * 添加费用规则
     *
     * @param feeRule
     * @return
     */
    @Override
    public int saveFeeRule(FeeRule feeRule) {
        //判断费用规则名称是否重复
        long count = this.count(new LambdaQueryWrapper<FeeRule>().eq(FeeRule::getName, feeRule.getName()));
        if (count > 0) {
            throw new ServiceException("该费用规则已经存在");
        }
        feeRule.setCreateBy(SecurityUtils.getUsername());
        feeRule.setCreateTime(new Date());
        feeRule.setUpdateTime(new Date());
        return feeRuleMapper.insert(feeRule);
    }

    /**
     * 修改费用规则
     *
     * @param feeRule
     * @return
     */
    @Override
    public int updateFeeRule(FeeRule feeRule) {
        //获取就的费用规则数据
        FeeRule oldFeeRule = this.getById(feeRule.getId());
        //判断要修改的费用规则数据是否存在
        if (oldFeeRule == null) {
            throw new ServiceException("要修改的费用规则不存在");
        }

        //判断费用规则名称是否重复
        if (!feeRule.getName().equals(oldFeeRule.getName())) {
            long count = this.count(new LambdaQueryWrapper<FeeRule>().eq(FeeRule::getName, feeRule.getName()));
            if (count > 0) {
                throw new ServiceException("该费用规则已经存在");
            }
        }
        feeRule.setUpdateTime(new Date());
        feeRule.setUpdateBy(SecurityUtils.getUsername());
        return feeRuleMapper.updateById(feeRule);
    }

    /**
     * 查询所有费用规则
     *
     * @return
     */
    @Override
    public List<FeeRule> selectAllRuleFeeList() {
        return feeRuleMapper.selectList(new LambdaQueryWrapper<FeeRule>().eq(FeeRule::getStatus, "1"));
    }

    /**
     * 计算订单费用
     *
     * @param requestForm
     * @return
     */
    @Override
    public FeeRuleResponseVo calculateOrderFee(FeeRuleRequestForm requestForm) {
        //封装传入的对象
        FeeRuleRequest feeRuleRequest = new FeeRuleRequest();
        feeRuleRequest.setDurations(requestForm.getDuration());
        log.info("传入参数：{}", JSON.toJSONString(feeRuleRequest));

        //开启会话
        KieSession kieSession = kieContainer.newKieSession();

        //封装要返回的对象
        FeeRuleResponse feeRuleResponse = new FeeRuleResponse();
        kieSession.setGlobal("feeRuleResponse", feeRuleResponse);
        //设置订单对象
        kieSession.insert(feeRuleRequest);
        //触发规则
        kieSession.fireAllRules();

        log.info("返回参数：{}", JSON.toJSONString(feeRuleResponse));

        //关闭会话
        kieSession.dispose();
        return getFeeRuleResponseVo(feeRuleResponse);
    }

    private static FeeRuleResponseVo getFeeRuleResponseVo(FeeRuleResponse feeRuleResponse) {
        FeeRuleResponseVo feeRuleResponseVo = new FeeRuleResponseVo();
        feeRuleResponseVo.setTotalAmount(BigDecimal.valueOf(feeRuleResponse.getTotalAmount()));
        feeRuleResponseVo.setExceedPrice(BigDecimal.valueOf(feeRuleResponse.getExceedPrice()));
        feeRuleResponseVo.setFreePrice(BigDecimal.valueOf(feeRuleResponse.getFreePrice()));
        feeRuleResponseVo.setExceedDescription(feeRuleResponse.getExceedDescription());
        feeRuleResponseVo.setFreeDescription(feeRuleResponse.getFreeDescription());
        return feeRuleResponseVo;
    }


}
